define(function (require, exports, module) {
	require('/includes/libraries/javascript/placeholder.js');
	require('/includes/libraries/javascript/validation.js');
	require('/includes/libraries/javascript/suggest');
	require('/includes/libraries/javascript/dialog');
	require('/includes/libraries/javascript/jquery.tinyscrollbar');

	var capslock = require('/includes/libraries/javascript/capslock');
	// input
	var form = $("#ys-form");

	function init() {
		initPlaceholer();


		$("#QTY").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});
		$("#SHIP_OVOY").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});
		$("#OVOY").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});
		$("#TD_NO").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});
		$("#USE_DD").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});
		
		$("#sname").click(function(){
			$(this).removeClass("form-item-error");
		}).keydown(function(){
			$(this).removeClass("form-item-error");
		});

		$("[id^='t_']").click(function() {
			$("[id^='t_']").removeClass("on");
			$(this).addClass("on");
			$("#XX").val($(this).attr("v"));
		});


		$("#minus").click(function(){
			var n=$("#QTY").val();

			var re = /^[0-9]*[1-9][0-9]*$/ ; 
			if(!re.test(n)){
				if(!isNaN(n)&&n!=""){
					n=parseInt(n);
				}else{
					n=1;
				}
			}else{
				if(n>1){
					n--;
				}
			}
			$("#QTY").val(n);
		});

		$("#plus").click(function(){
			var n=$("#QTY").val();

			var re = /^[0-9]*[1-9][0-9]*$/ ; 
			if(!re.test(n)){
				if(!isNaN(n)&&n!=""){
					n=parseInt(n);
				}else{
					n=1;
				}
			}else{
				if(n<100){
					n++;
				}
			}
			$("#QTY").val(n);
		});

		$("#QTY").keyup(function(){
			var n=$(this).val();

			var re = /^[0-9]*[1-9][0-9]*$/ ; 
			if(!re.test(n)){
				if(!isNaN(n)&&n!=""){
					n=parseInt(n);
				}else{
					n=1;
				}
			}
			if(n>100){
				n=100;
			}

			$(this).val(n);
		});
	}

	//表单验证

	function showsuc(sysmsg){
		if(sysmsg){
			$("#syss").text(sysmsg);
			$("#syss").slideDown();

			setTimeout(function(){
				$("#syss").slideUp();
			},2000);
		}
	}

	function showerror(sysmsg,id){
		if(sysmsg){
			$("#syse").text(sysmsg);
			$("#syse").slideDown();

			setTimeout(function(){
				$("#syse").slideUp();
			},2000);
		}
		if(id!=undefined){
			$("#"+id).addClass("form-item-error");
			$("#"+id).focus();
		}
	}

	function validate() {

		var XX=$("#XX").val();
		var QTY=$("#QTY").val();
		var SHIP_OVOY=$("#SHIP_OVOY").val();
		var OVOY=$("#OVOY").val();
		var TD_NO=$("#TD_NO").val();
		var USE_DD=$("#USE_DD").val();
		var EUSE_DD=$("#EUSE_DD").val();
		
		var CUS_NO=$("#CUS_NO").val();
		if(CUS_NO==undefined||CUS_NO==null||CUS_NO==""){
			showerror("船公司无效","sname");
			return false;
		}
		
		if(XX==undefined||XX==null||XX==""){
			showerror("请选择箱型","XX");
			return false;
		}

		if(QTY==undefined||QTY==null||QTY==""||parseInt(QTY)<=0||isNaN(QTY)){
			showerror("请输入正确的箱量","QTY");
			return false;
		}
		if(SHIP_OVOY==undefined||SHIP_OVOY==null||SHIP_OVOY==""){
			showerror("请输入船名","SHIP_OVOY");
			return false;
		}
		if(OVOY==undefined||OVOY==null||OVOY==""){
			showerror("请输入航次","OVOY");
			return false;
		}
		if(TD_NO==undefined||TD_NO==null||TD_NO==""){
			showerror("请输入提单号","TD_NO");
			return false;
		}
		if(USE_DD==undefined||USE_DD==null||USE_DD==""){
			showerror("请选择用箱开始时间","USE_DD");
			return false;
		}
		if(EUSE_DD==undefined||EUSE_DD==null||EUSE_DD==""){
			showerror("请选择用箱截止时间","EUSE_DD");
			return false;
		}
		return true;
	}
	var busy=false;
	//提交表单
	function formSubmit(form) {
		if(busy){
			return false;
		}
		busy=true;
		$btnRegister = $("#btn-submit");

		var param = $(form).serialize();
		var ajaxurl = '/index.php?';
		$.ajax({
			type: 'post',
			url: ajaxurl + location.search.substring(1),
			contentType: "application/x-www-form-urlencoded; charset=utf-8",
			data: param,
			cache:false,
			beforeSend: function () {
				$btnRegister.text('正在预定..');
			},
			error: function () {
				busy=false;
				showDialog('网络繁忙，请稍后再试');
			},
			success: function (response) {
				if (response) {
					try{
						var obj = eval(response);

						if (obj.success) {
							successRedirectURL(obj.success);
						}

						if (obj.info) {
							busy=false;
							showerror(obj.info);
							$btnRegister.text('加入要箱');
						}
					}
					catch(e){busy=false;showerror('发生错误，请重试');$btnRegister.text('加入要箱');}

				}
				$btnRegister.text('加入要箱');
			}
		});
	}

	$("#btn-submit").click(function(){
		if(validate()){
			formSubmit("#ys-form");
		}
	});

	function initPlaceholer() {
		$('input[placeholder]').placeholder({
			isValue: true,
			topDiff: 1
		});
	}

	function showDialog(content) {
		$('body').dialog({
			title: '提示',
			fixed: true,
			width: 380,
			//height: 150,
			maskClose: true,
			type: 'html',
			source: '<div class="registerDialog">\
				<div class="ico"></div>\
				<div class="con">\
				' +
				content +
				'\
				</div></div>'
		})
	}
	exports.init = init;
})
function successRedirectURL(url){
	var isIE = !-[1,];
	if(isIE){
		var link = document.createElement("a");
		link.href = url;
		link.style.display = 'none';
		document.body.appendChild(link);
		link.target = "_top";
		link.click();
	}else{
		window.top.location = url;
	}
}